# TextMenuItem - Simple menuitem that contains a text label (and optional extra)
# Copyright (C) 2007 Lauri Taimila
# 
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

__licence__ = "GPLv2"
__copyright__ = "2007, Lauri Taimila"
__author__ = "Lauri Taimila <lauri@taimila.com>"

import pango
import clutter

from frontend.gui.widgets.menu_item import MenuItem

class TextMenuItem(MenuItem):
    """
    Simple menuitem widget that contains text label.
    """
    
    # Offset in pixels
    OFFSET = 20
    
    def __init__(self, width, height, theme, label_text, extra_text=None):
        """
        Initialize menuitem widget.
        
        @param width: Widget width in pixels
        @param height: Widget height in pixels
        @param theme: Theme object
        @param label_text: Label text as a string
        @param extra_text: String that contains extra information.
        """
        MenuItem.__init__(self, width, height)

        self.active_text_color = theme.getColor("menuitem_active")
        self.inactive_text_color = theme.getColor("menuitem_inactive")
        
        # Set label text
        try:
            first_line = label_text[:label_text.index('\n')]
        except ValueError:
            first_line = label_text
        label = clutter.Label()
        label.set_name("text_label")
        label.set_text(first_line)
        label.set_color(self.inactive_text_color)
        label.set_font_name(theme.getFont() + " 28px")
        label.set_line_wrap(False)
        label.set_ellipsize(pango.ELLIPSIZE_END)
        label.show()
        label.set_position(self.OFFSET, 
                           int((height - label.get_height()) / 2))
        self.add(label)
        
        # Set extra text
        if extra_text is not None:
            extra = clutter.Label()
            extra.set_name("text_label")
            extra.set_text(extra_text)
            extra.set_font_name(theme.getFont() + " 28px")
            extra.set_color(self.inactive_text_color)
            extra.show()
            extra.set_position(width - extra.get_width() - self.OFFSET, 
                               int((height - label.get_height()) / 2))
            self.add(extra)
            
            # Ellipsize label if it's too long. (Cut label and add three dots)
            if label.get_width() > (width - extra.get_width() - (self.OFFSET * 2 + (self.OFFSET / 2))):
                label.set_width(width - extra.get_width() - (self.OFFSET * 2 + (self.OFFSET / 2)))
        else:
            # Ellipsize label if it's too long. (Cut label and add three dots)
            if label.get_width() > (width - (self.OFFSET * 2 )):
                label.set_width(width - (self.OFFSET * 2 ))
                
    def set_active(self, boolean):
        """
        Set menuitem active or inactive. Changes the text color of the menuitem.
        @param boolean: use True to set active and False to set inactive
        """
        MenuItem.set_active(self, boolean)
        if boolean:
            for index in range(self.get_n_children()):
                actor = self.get_nth_child(index)
                if actor.get_name() == "text_label":
                    actor.set_color(self.active_text_color)
        else:
            for index in range(self.get_n_children()):
                actor = self.get_nth_child(index)
                if actor.get_name() == "text_label":
                    actor.set_color(self.inactive_text_color)
